package dapps.web.document;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.web.struts2.CRUDActionSupport;

import dapps.entity.document.Category;
import dapps.service.ServiceException;
import dapps.service.document.CategoryManager;

@SuppressWarnings("serial")
@Results( { @Result(name = CRUDActionSupport.RELOAD, location = "category.action?page.pageRequest=${page.pageRequest}", type = "redirect")})
public class CategoryAction extends CRUDActionSupport<Category> {

	@Autowired
	private CategoryManager categoryManager;

	// 基本属性
	private Category entity;
	private Long id;
	private Page<Category> page = new Page<Category>(15);//每页5条记录
	private String url;  
	  
	

	// 基本属性访问函数 //

	public Category getModel() {
		return entity;
	}
	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = categoryManager.get(id);
		} else {
			entity = new Category();
		}
	}
	public String getUrl() {  
	     return url;  
	}  
	public void setId(Long id) {
		this.id = id;
	}

	public Page<Category> getPage() {
		return page;
	}

	// CRUD Action 函数 //

	@Override
	public String list() throws Exception {
		page = categoryManager.getAll(page);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		categoryManager.save(entity);	
		addActionMessage("保存文档分类成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		try {
			categoryManager.delete(id);	
			addActionMessage("删除文档分类成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage(e.getMessage());
		}
		return RELOAD;
	}

		
}
